# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:android)

platform :android do
  desc "Runs all the tests"

  lane :demo do
    gradle(
      task: "assemble",
      flavor: '',
      build_type: 'Profile')
  end

  lane :test do
    # 更新代码
    # git_pull(rebase: true)

    # 更新项目依赖
    puts `flutter pub get`

    # 生成中间文件

    gradle(
      task: "clean assemble",
      build_type: 'Profile')

    filePath = File.join(Dir.pwd, '../../', 'build/app/outputs/apk/profile/app-profile.apk')

    # 提取 git 日志，以序号的形式
    messages = Array.new
    messages << "分支： #{git_branch}"
    logs = changelog_from_git_commits(
      commits_count: 1,
      pretty: "- (%ae) %s",
      date_format: "short",
      match_lightweight_tag: false,
      merge_commit_filtering: "exclude_merges"
    ).split(/[\n]/).each_with_index {|item, index|
      commitMessage = item.strip.gsub(/[;|；]/, '') + ";"
      if !commitMessage.empty?
        orderMessage = commitMessage.split(/[:|：]|([:|：][[:space:]])/)
        if orderMessage.size > 1
          messages << "#{(index+1).to_s + '. '}" + orderMessage[1]
        else
          messages << "#{(index+1).to_s + '. '}" + orderMessage.shift.to_s
        end
      end
    }

    branch = git_branch

    # 上传蒲公英以及发企业微信消息
    pgyer(
      apk: filePath,
      api_key: "3809815e45d087d7a2cf2d9eb9c772b4",
#       password: "123456",
      install_type: "1",
      update_description: "\n#{messages.join("\n")}")

    apk_file_path = lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH];

    puts("apk file path: #{apk_file_path}")

    # 发送电脑通知
    notification(title: "#{branch}", subtitle: "测试包已上传至蒲公英，打包完成！", message: "\n#{messages.join("\n")}")
  end

  desc "Deploy a new version to the  oss cloud"
  lane :stage do
    # 更新代码
    # git_pull(rebase: true)

    # 更新项目依赖
    puts `flutter pub get`

    # 生成中间文件

    gradle(
      task: "clean assemble",
      build_type: 'Release')

    filePath = File.join(Dir.pwd, '../../', 'build/app/outputs/apk/release/app-release.apk')


    # apk_file_path = lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH];
    # puts("apk file path: #{apk_file_path}")

    upload_oss_action(
      file_path:filePath,
      version:"v886"
    )

    # 发送电脑通知
    notification(title: "打包完成", subtitle: "正式包已上传至云平台，打包完成！", )
  end

  desc "Deploy a new version to the Google Play"
  lane :release do

    # 更新代码
#     git_pull(rebase: true)

    # 更新项目依赖
    puts `flutter pub get`

    gradle(
      task: "clean assembleRelease",
      build_type: 'Release')

    filePath = File.join(Dir.pwd, '../../', 'build/app/outputs/apk/release/app-release.apk')
    # 上传蒲公英以及 webhook 触发的钉钉消息
    # 可以自动提取 git 序号形式的日志
    messages = Array.new
    messages << "分支： #{git_branch}"
    logs = changelog_from_git_commits(
      commits_count: 1,
      pretty: "- (%ae) %s",
      date_format: "short",
      match_lightweight_tag: false,
      merge_commit_filtering: "exclude_merges"
    ).split(/[\n]/).each_with_index {|item, index|
      commitMessage = item.strip.gsub(/[;|；]/, '') + ";"
      if !commitMessage.empty?
        orderMessage = commitMessage.split(/[:|：]|([:|：][[:space:]])/)
        if orderMessage.size > 1
          messages << "#{(index+1).to_s + '. '}" + orderMessage[1]
        else
          messages << "#{(index+1).to_s + '. '}" + orderMessage.shift.to_s
        end
      end
    }

    branch = git_branch

    pgyer(
      apk: filePath,
      api_key: "3809815e45d087d7a2cf2d9eb9c772b4",
#       password: "123456",
      install_type: "1",
      update_description: "\n#{messages.join("\n")}")

      # 发送电脑通知
    notification(title: "#{branch}", subtitle: "测试包已上传至 google play，打包完成！", message: "\n#{messages.join("\n")}")

    upload_to_play_store(
      apk: filePath,
      skip_upload_metadata: true,
      skip_upload_changelogs: true)
  end
end
